<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<meta name="renderer" content="webkit">
		<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
		<meta name="viewport" content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi" />
		<link rel="shortcut icon" href="/favicon.ico" type="image/x-icon" />
		<link rel="stylesheet" href="/css/font.css">
		<link rel="stylesheet" href="/css/xadmin.css">
	</head>
	<body>
		<div class="x-nav">
			<span class="layui-breadcrumb">
				<a href="#">学校通知管理</a>
			</span>
			<a class="layui-btn layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right" href="javascript:location.replace(location.href);"
			 title="刷新">
				<i class="layui-icon" style="line-height:30px">ဂ</i></a>
		</div>
		<div class="x-body" id="app">
			<xblock>
				<button class="layui-btn layui-btn-danger" onclick="delAll()"><i class="layui-icon"></i>批量删除</button>
				<button class="layui-btn" onclick="open_addMessage()"><i class="layui-icon"></i>发布消息</button>
				<span class="x-right" style="line-height:40px">共有数据：{{ pageInfo.total }} 条，当前{{ pageInfo.pageNum }} / {{ pageInfo.pages }}页</span>
			</xblock>
			<table class="layui-table">
				<thead>
					<tr>
						<th width="20">
							<div class="layui-unselect header layui-form-checkbox" lay-skin="primary"><i class="layui-icon">&#xe605;</i></div>
						</th>
						<th width="40">序号</th>
						<th>标题</th>
						<th>消息</th>
						<th width="60">消息类型</th>
						<th width="60">是否置顶</th>
						<th width="150">操作</th>
				</thead>
				<tbody>
					<tr v-for="(message,index) in messages">
						<td>
							<div class="layui-unselect layui-form-checkbox" lay-skin="primary" :data-id="message.id" :data-title="message.title" ><i class="layui-icon">&#xe605;</i></div>
						</td>
						<td>{{ index+1 }}</td>
						<td>{{ message.title }}</td>
						<td style="max-width: 160px;overflow: hidden;text-overflow:ellipsis;white-space: nowrap;">{{ message.txt }}</td>
						<td v-if="message.type == 1">学校</td>
						<td v-if="message.type == 2">教师</td>
						<td v-if="message.type == 3">学生</td>
						<td v-if="message.top == 0">不置顶</td>
						<td v-if="message.top == 1">置顶</td>
						<td class="td-manage">
							<button class="layui-btn layui-btn layui-btn-sm" onclick="open_editMessage(this)"><i class="layui-icon">&#xe642;</i>编辑</button>
							<button class="layui-btn-danger layui-btn layui-btn-sm" onclick="member_del(this)" href="javascript:;"><i
								 class="layui-icon">&#xe640;</i>删除</button>
						</td>
					</tr>
				</tbody>
			</table>
			<div class="page" style="margin-top: 10px !important;">
               <div>
                 <a class="prev pageTxt" href="javascript:void(0)">上一页</a>
                 <a class="first pageTxt" href="javascript:void(0)">首页</a>
                 <template v-for="pageNum in pageInfo.navigatepageNums">
                     <a class="num" href="javascript:void(0)" v-if="pageInfo.pageNum != pageNum" :data="pageNum">{{ pageNum }}</a>
                     <span class="current" v-else :data="pageNum">{{ pageNum }}</span>
                 </template>
                 <a class="last pageTxt" href="javascript:void(0)">尾页</a>
                 <a class="next pageTxt" href="javascript:void(0)">下一页</a>
               </div>
             </div>
		</div>
		<script type="text/javascript" src="/js/jquery.min.js"></script>
        <script type="text/javascript" src="/lib/layui/layui.js" charset="utf-8"></script>
        <script type="text/javascript" src="/js/xadmin.js"></script>
		<script type="text/javascript" src="/js/vue.min.js"></script>
        <script type="text/javascript" src="/js/jquery.form.js"></script>
		<script>
		
			var vm = new Vue({
	            el : '#app',
	            data : {
	                pageInfo:"",
	                messages:""
	            },
	            created : function() {
	                // 获取学校通知消息
	                this.listMessage(1);
	            },
	            methods : {
	            	listMessage(pageNum) {
	                    var vm = this;
	                    $.ajax({
	                        url : "/sys/message/listMessage",
	                        data:{
	                            pageNum:pageNum
	                        },
	                        type : "get",
	                        success : function(result) {
	                            if (result.state) {
	                                vm.pageInfo = result.data;
	                                vm.messages = result.data.list;
	                            } else {
	                                layui.use(['layer'], function(){
	                                    var layer = layui.layer
	                                    layer.msg(""+result.msg, {icon: 5});
	                                });
	                            }
	                        }
	                    });
	                }
	            }
	        });
			
			// 发布消息
            function open_addMessage() {
                x_admin_show('发布消息','/sys/message/toAddMessage', 600, 430);
            }
            // 编辑消息
            function open_editMessage(obj) {
            	var messageId = $(obj).parents("tr").find("div:first").attr("data-id");
                x_admin_show('编辑','/sys/message/toEditMessage?messageId=' + messageId, 600, 430);
            }
			
		
			// 点击页码数，进行跳转
            $(".page").delegate(".num", "click", function() {
                var pageNum = $(this).attr("data");
                vm.listMessage(pageNum);
            });
            // 上一页
            $(".page").delegate(".prev", "click", function() {
                if (vm.pageInfo.hasPreviousPage) {
                    vm.listMessage(vm.pageInfo.pageNum - 1);
                }
            });
            // 下一页
            $(".page").delegate(".next", "click", function() {
                if (vm.pageInfo.hasNextPage) {
                    vm.listMessage(vm.pageInfo.pageNum + 1);
                }
            });
            // 首页
            $(".page").delegate(".first", "click", function() {
                if (!vm.pageInfo.isFirstPage) {
                    vm.listMessage(1);
                }
            });
            // 尾页
            $(".page").delegate(".last", "click", function() {
                if (!vm.pageInfo.isLastPage) {
                    vm.listMessage(vm.pageInfo.pages);
                }
            });

			// 单个删除
			function member_del(obj) {
				var title = $(obj).parents("tr").find("div:first").attr("data-title");
				var id = $(obj).parents("tr").find("div:first").attr("data-id");
				var messageIds = [];
				messageIds.push(id);
				layer.confirm('确认要删除【' + title + '】吗？', function(index) {
					$.ajax({
						url: "/sys/message/delMessage",
						type: "post",
						data: {
							messageIds: messageIds
						},
						success: function(result) {
							if (result.state) {
								layer.msg('删除成功!', {
									icon: 1,
									time: 1000
								});
								// 根据当前页面数据量决定加载前一页还是当前页
								if (vm.pageInfo.size - 1 == 0 && vm.pageInfo.pageNum != 1) {
									vm.listMessage(vm.pageInfo.pageNum - 1);
								} else {
									vm.listMessage(vm.pageInfo.pageNum);
								}
							} else {
								layer.msg("" + result.msg, {
									icon: 5
								});
							}
						}
					});
				});
			}

			// 批量删除
			function delAll(argument) {
				var messageIds = tableCheck.getData();
				if (messageIds.length <= 0) {
					return;
				}
				// 获取选中的学校通知标题
				var obj = $(".layui-form-checked").not('.header');
				var title = [];
				obj.each(function(index, el) {
					title.push(obj.eq(index).attr('data-title'));
				});
				layer.confirm('确认要删除【' + title + '】吗？', function(index) {
					$.ajax({
						url: "/sys/message/delMessage",
						type: "post",
						data: {
							messageIds: messageIds
						},
						success: function(result) {
							if (result.state) {
								layer.msg('删除成功', {
									icon: 1
								});
								// 移除所有checkbox的勾选状态
								$(".layui-form-checkbox").removeClass('layui-form-checked');
								// 根据当前页面数据量决定加载前一页还是当前页
								if (vm.pageInfo.size - messageIds.length == 0 && vm.pageInfo.pageNum != 1) {
									vm.listMessage(vm.pageInfo.pageNum - 1);
								} else {
									vm.listMessage(vm.pageInfo.pageNum);
								}
							} else {
								layer.msg("" + result.msg, {
									icon: 5
								});
							}
						}
					});
				});
			}
			// 保存成功回调
			function updateData() {
				 vm.listMessage(1);
			}
			// 编辑成功回调
			function updateEditData() {
				vm.listMessage(vm.pageInfo.pageNum);
			}
		</script>
	</body>

</html>
